#!/usr/bin/env python
# -*- coding:utf-8 -*-


from selenium import webdriver
from mylog import MyLog


class Item(object):
    ip = None
    port = None
    anonymous = None
    category = None
    location = None
    response_speed = None
    last_varify_time = None


class GetProxy(object):
    def __init__(self):
        self.base_url = 'http://www.kuaidaili.com/free/'
        self.urls = []
        self.items = []
        self.log = MyLog()

        self.urls = self.get_urls(self.base_url)
        self.items = self.spider(self.urls)
        self.pipelines(self.items)


    def get_urls(self, base_url):
        # http://www.kuaidaili.com/free/inha/3/
        urls = []
        for i in xrange(1, 11):
            url = base_url + 'inha/' + str(i)
            urls.append(url)
            self.log.info('Get A Url: %s' % url)

        return urls


    def spider(self, urls):
        browser = webdriver.PhantomJS()
        items = []
        for url in urls:
            browser.get(url)
            browser.implicitly_wait(5)
            elements = browser.find_elements_by_xpath('//tbody/tr')
            for ele in elements:
                item = Item()
                ele_info = browser.find_elements_by_xpath('.//td')
                self.log.info('Get A Proxy')
                item.ip = ele_info[0].text.encode('utf8')
                self.log.info(item.ip)
                item.port = ele_info[1].text.encode('utf8')
                self.log.info(item.port)
                item.anonymous = ele_info[2].text.encode('utf8')
                self.log.info(item.anonymous)
                item.category = ele_info[3].text.encode('utf8')
                self.log.info(item.category)
                item.location = ele_info[4].text.encode('utf8')
                self.log.info(item.location)
                item.response_speed = ele_info[5].text.encode('utf8')
                self.log.info(item.response_speed)
                item.last_varify_time= ele_info[6].text.encode('utf8')
                self.log.info(item.last_varify_time)
                items.append(item)

        return items


    def pipelines(self, items):
        filename = 'proxy.txt'
        with open(filename, 'w') as fp:
            for item in items:
                fp.write(item.ip + '\n')
                fp.write(item.port + '\n')
                fp.write(item.anonymous + '\n')
                fp.write(item.category + '\n')
                fp.write(item.location + '\n')
                fp.write(item.response_speed + '\n')
                fp.write(item.last_varify_time + '\n')
                fp.write('\n')

        self.info.info('Write To File %s Success' % filename)


if __name__ == '__main__':
    GP = GetProxy()
